﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using ss = System.Speech.Synthesis;

namespace PC_To_XBEE_Test_1
{
    class Program
    {
        // Initialize a new instance of the SpeechSynthesizer.
        static ss.SpeechSynthesizer synth = new ss.SpeechSynthesizer();
            
        static void Main(string[] args)
        {
            // Speak a string.
            synth.Speak("Nagi, welcome");

            string cmd = "";
            SerialPort xbee = new SerialPort("COM9", 9600);
            xbee.DataReceived += new SerialDataReceivedEventHandler(xbee_DataReceived);
            xbee.ErrorReceived += new SerialErrorReceivedEventHandler(xbee_ErrorReceived);

            xbee.Open();
            Console.WriteLine(xbee.IsOpen ? "Open" : "Not Yet");
            synth.Speak("COM Open");
            while (cmd != "Q")
            {
                cmd = Console.ReadLine();

                if (xbee.IsOpen && cmd !="Q")
                {
                    Console.WriteLine("SENDING :: [" + cmd + "] " + cmd.Length);
                    xbee.WriteLine(cmd);
                }
            }
            xbee.Close();
            
        }

        static void xbee_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {
            Console.WriteLine("ERR :: " + e.EventType);

        }

        static void xbee_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort xbee = (SerialPort) sender;
            string s = xbee.ReadLine().Trim();
            Console.WriteLine("RECEIVED :: [" +  s + "] " + s.Length);
            synth.Speak("RECEIVED " + s);
        }
    }
}
